<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-28
*/
/**
 * 
 * 轮播器控制器
 * @author xjh
 *
 */
class TurnController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array(''),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}

	/**
	 * 顶部信息
	 * 
	 */
	function actionturntop(){
		$this->renderPartial('turntop');
	}

	/**
	 * 导航删除
	 */
	function actionTurnDel($turn_id){
		$turn_model = Turn::model();
		$row = $turn_model->TurnDel($turn_id);
		if($row){
			echo 1;
			exit;
		}else{
			echo 0;
			exit;
		}
	}

	/**
	 * 轮播修改
	 */
	function actionturnupd($turn_id){
		$turn_model = new Turn();
		$turn_info = $turn_model->getTurnById($turn_id);
		if(isset($_POST) && @$_POST['send'] == "修改"){
			$row = $turn_model->TurnUpdate($_POST,$turn_id);
			if($row){
				$this->redirect("./index.php?r=backweb/turn/turn");
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updateturn',array('turn_info'=>$turn_info[0]));
	}
	
	
	/**
	 * 是否有效的切换
	 */
	function actionActive($turn_id,$active_id){
		$active = new active();
		$active -> setActive($turn_id,$active_id,"turn","turn_id");
		$this->redirect("./index.php?r=backweb/turn/turn");
	}
	
	/**
	 *轮播器展示
	 * @author xjh
	 */
	function actionturn(){
		$nav_model = Nav::model();
		$turn_model = Turn::model();
		if(@$_POST['send'] == '更新信息'){
			for ($i=0; $i < count($_POST['turn_id']); $i++) { 
				$row = $turn_model -> updateTurn($_POST['turn_id'][$i],$_POST['sort'][$i]);
			}
		}
		if(@$_POST['btquery'] == '查询'){
			$infos = $turn_model->pageSearch($_POST);
		}else{
			$infos = $turn_model->all();
		}
		$infos_count = $nav_model->infos_count;
		$active = array(
			0=>'否',
			1=>'是',
		);
		$obj = new CDbConnection();
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$this->renderPartial('turn',array('infos'=>$infos,'active'=>$active,'info_count'=>$infos_count[0]['count(*)'],'number'=>$number,'times'=>$times));
	}
	
	function actiontoAddTurn(){
		$this->renderPartial('turnadd');
	}
	/*
	 * 轮播图添加控制器
	 * @author xjh
	 */
	function actionAddTurn(){
		
			$image = new Image($_POST['turn_url']);
			$image->fixPro(1800,570);
			$image->out();
			
		$turn_model = new Turn();
			if($_POST['btsave'] == '添加'){
					if($turn_model->addTurn($_POST)){
						echo "<script>alert('轮播图添加成功');</script>";
						 $this->renderPartial('turntop');
					}else{
						echo "<script>alert('添加失败');</script>";
					}
			}else{
				echo "非法操作,请返回继续操作";
				exit;
			}
	}
	
	//单文件上传
	function actionthumbnail(){
		$this->renderPartial('thumbnail');
	}
	
	//单文件上传处理
	function actionuploadfile(){ 
		if(isset($_POST['send'])){
			$_fileupload = new upload('pic',$_POST['MAX_FILE_SIZE']);
			$this->alertOpenerClose("缩略图上传成功",$_fileupload->getPath());
		}else{
			echo "<script>alert('文件过大,请重新上传')</script>";
			exit;
		}
	}
	
	//上传成功返回填写地址
	function alertOpenerClose($_info,$_path){
		echo "<script type='text/javascript'>alert('$_info');</script>";
		echo "<script type='text/javascript'>opener.document.fm.turn_url.value='$_path';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.style.display='block';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.src='$_path';</script>";
		echo "<script type='text/javascript'>window.close()</script>";
		exit();
	}
}
?>